package servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Database.MySQLController;
import Model.Staff;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * Servlet implementation class RemoveStaff
 */
@WebServlet("/RemoveStaff")
public class RemoveStaff extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RemoveStaff() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("HELLO WORLD!");
		// check if staff holding appointments at server and client side
		// stop deletion of staff is said staff has existing appointments
		String[] loginIDs = request.getParameterValues("loginID");
		if (loginIDs != null) {
			for (String loginID : loginIDs) {
				System.out.println(loginID);
			}
		}
		String serverPath = "http://" + request.getServerName() + ":"
				+ request.getServerPort()
				+ request.getServletContext().getContextPath();
		generateReport(serverPath, request, response);
	}

	protected void generateReport(String serverPath,
			HttpServletRequest request, HttpServletResponse response) {
		Document myDocument = new Document(PageSize.A4, 36, 36, 30, 70);
		try {
			PdfWriter.getInstance(myDocument, response.getOutputStream());
			myDocument.setMarginMirroring(true);
			myDocument.addAuthor("QMS System");
			myDocument.addCreator("FYPJ");
			myDocument.addTitle("List of Staff that Left");
			myDocument.open();
			AppAllPrint ap = new AppAllPrint();
			myDocument.add(ap.addLogo(serverPath
					+ "/images/The_Nanyang_Polytechnic_Logo.jpg"));
			Font fontbold = new Font();
			fontbold.setStyle(Font.BOLD);
			Paragraph paragraph = new Paragraph("Staff Flagged for Removal",
					fontbold);
			myDocument.add(generateTableList());
			myDocument.add(Chunk.NEWLINE);
			Font header = new Font();
			header.setStyle(Font.BOLD | Font.UNDERLINE);
			myDocument.add(new Paragraph("Actions Required : ",header));
			ArrayList<Staff> staffList = new ArrayList<Staff>();
			staffList = Staff.getStaffOfStatus("Left");
			for(Staff staff : staffList)
			{
				if(staff.isStaffHoldingAppointments())
				{
					String msg = staff.getName() + " is found to be"
							+ " holding appointments and cannot be removed from"
							+ " the system!\n";
					myDocument.add(new Phrase(msg));
				}
			}
			myDocument.close();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private PdfPTable generateTableList() {
		ArrayList<Staff> stafflist = new ArrayList<Staff>();
		stafflist = Staff.getStaffOfStatus("Left");
		Font baseFont = FontFactory.getFont(FontFactory.COURIER_BOLD);
		PdfPTable staffTable = new PdfPTable(4);
		staffTable.setSpacingBefore(20.0f);
		staffTable.setSpacingAfter(20.0f);
		staffTable.addCell(new Phrase("Staff Name"));
		staffTable.addCell(new Phrase("Staff Email"));
		staffTable.addCell(new Phrase("Outstanding Appointments"));
		staffTable.addCell(new Phrase("Requires Followup"));
		for (Staff staff : stafflist) {
			staffTable.addCell(new Phrase(staff.getName()));
			staffTable.addCell(new Phrase(staff.getEmail()));
			if(staff.isStaffHoldingAppointments())
			{
				PdfPCell appointmentCell = new PdfPCell();
				ArrayList<String> appointments = new ArrayList<String>();
				appointments = staff.getAppointmentsForStaff();
				String tempHolderForAppointmentNames = "";
				for(String appointmentTitle : appointments)
				{
					tempHolderForAppointmentNames += appointmentTitle + "\n";
				}
				appointmentCell.addElement(new Phrase(tempHolderForAppointmentNames));
				staffTable.addCell(appointmentCell);
				PdfPCell colorCell = new PdfPCell(new Phrase("YES"));
				colorCell.setHorizontalAlignment(Rectangle.ALIGN_CENTER);
				colorCell.setBackgroundColor(BaseColor.RED);
				staffTable.addCell(colorCell);
			}else{
				staffTable.addCell("---------");
				PdfPCell noCell = new PdfPCell(new Phrase("NO"));
				noCell.setHorizontalAlignment(Rectangle.ALIGN_CENTER);
				noCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
			staffTable.addCell(noCell);
			}
		}
		return staffTable;
	}

}
